package com.sxf.spring.security;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.sxf.domain.viewbean.UserVO;
import com.sxf.querybean.UserQuery;
import com.sxf.service.UserService;

@Service("LocalUserDetailsService")
public class LocalUserDetailsService implements UserDetailsService {
	@Resource
	private UserService us;

	@Override
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException {
		UserQuery uq = new UserQuery();
		uq.setLoginName(username);
		List<UserVO> uvList = us.queryList(uq);
		if (uvList != null && !uvList.isEmpty()) {
			UserVO uv = uvList.get(0);
			return new CustomUserDetails(username, uv.getPassword(), "admin",
					uv.getDeleted());
		}
		return null;
	}

}
